﻿#pragma once

#include <qobject.h>
#include <qthread.h>
#include <qvector.h>
#include <qthreadpool.h>

//IO层
class FileReader;
class FileWriter;
class SerialPortManager;

//UI层
class MemsAutoTestSoftware;
class ParameterConsole;
class SetMEMSDataDialog;
class SetPositionModeDialog;
class SetRateModeDialog;
class SetScriptDialog;
class SetScriptLoopDialog;
class SetScriptWaitDialog;
class SetThermostatDialog;
class SetTurnTableAndThermostatDialog;

//功能层
class MEMSDataCodec;
class MEMSDataHandler;
class MEMSDataSaver;
class ScriptRunner;
class ScriptSaveLoader;
class TurnTableCodec;
class TurnTableConnectionStateManager;
class TurnTableRunningStateManager;
class ConfigSaveLoader;
class LogSaver;


class ObjectManager : public QObject
{
  Q_OBJECT

private:
  QVector<QThread*> threadPointerList;

  //IO层
  FileReader* fileReader = Q_NULLPTR;
  FileWriter* fileWriter = Q_NULLPTR;
  SerialPortManager* serialPortManager_turnTable = Q_NULLPTR;//转台串口管理器
  SerialPortManager* serialPortManager_mEMSData = Q_NULLPTR;//数据采集器的串口管理器
  //UI层
  MemsAutoTestSoftware* memsAutoTestSoftware = Q_NULLPTR;//串口助手主UI
  ParameterConsole* parameterConsole = Q_NULLPTR;//参数控制台（未实现）
  SetMEMSDataDialog* setMEMSDataDialog= Q_NULLPTR;//设置MEMS数据传输
  SetPositionModeDialog* setPositionModeDialog = Q_NULLPTR;//设置位置模式
  SetRateModeDialog* setRateModeDialog = Q_NULLPTR;//设置速率模式
  SetScriptDialog* setScriptDialog = Q_NULLPTR;//设置脚本
  SetScriptLoopDialog* setScriptLoopDialog = Q_NULLPTR;//设置脚本循环
  SetScriptWaitDialog* setScriptWaitDialog = Q_NULLPTR;//设置脚本等待
  SetThermostatDialog* setThermostatDialog = Q_NULLPTR;//设置温箱温度和速率
  SetTurnTableAndThermostatDialog* setTurnTableAndThermostatDialog = Q_NULLPTR;//设置转台参数
  //功能层
  MEMSDataCodec* mEMSDataCodec = Q_NULLPTR;//MEMS数据的编码解码器
  MEMSDataHandler* mEMSDataHandler = Q_NULLPTR;//MEMS数据处理
  MEMSDataSaver* mEMSDataSaver = Q_NULLPTR;//MEMS数据保存
  ScriptRunner* scriptRunner = Q_NULLPTR;//脚本运行
  ScriptSaveLoader* scriptSaveLoader = Q_NULLPTR;//脚本保存的载入
  TurnTableCodec* turnTableCodec = Q_NULLPTR;//转台编码解码器
  TurnTableConnectionStateManager* turnTableConnectionStateManager = Q_NULLPTR;//转台连接状态管理
  TurnTableRunningStateManager* turnTableRunningStateManager = Q_NULLPTR;//转台状态管理器
  ConfigSaveLoader* configSaveLoader = Q_NULLPTR;//配置加载和保存
  LogSaver* logSaver = Q_NULLPTR;//日志保存器

  QThread* qThread_serialPortManager_turnTable = Q_NULLPTR;//转台串口管理器线程
  QThread* qThread_serialPortManager_dataReader = Q_NULLPTR;//数据采集器的串口管理器线程
  QThreadPool* qThreadPool = Q_NULLPTR;//全局线程池
  //构造函数、析构函数、复制函数
public:
  ObjectManager(QObject* parent=Q_NULLPTR);
  ~ObjectManager();

  //初始化函数，暂不开放该接口
private:
  void initModules();
  void objectInjection();
  void moveToThread();

  //代码优化
private:
  //将对象移动到指定的线程
  void moveObjectToThread(QObject* object, QThread* thread);
  void addThreadToList(QThread* thread);
};
